package com.jdjw.modules.thirdpost.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdjw.common.ResponseResult;
import com.jdjw.modules.thirdpost.mapper.BusProcureApplyEntMapper;
import com.jdjw.modules.thirdpost.param.BusAssetApplyTouBiaoParam;
import com.jdjw.modules.thirdpost.param.ThirdPostInParm;
import com.jdjw.modules.thirdpost.result.BusProcureApplyEnt;
import com.jdjw.utils.CommonUtil;
import com.jdjw.utils.ConstantUtil;
import com.jdjw.utils.DateUtil;
import com.jdjw.utils.HttpRequestUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * 投标单位
 */
@Service
@Slf4j
public class BusAssetApplyTouBiaoSetviceImpl extends ServiceImpl<BusProcureApplyEntMapper, BusProcureApplyEnt>
        implements IService<BusProcureApplyEnt> {

    @Autowired
    static BusProcureApplyEntMapper busProcureApplyEntMapper;

    @Autowired
    static ThirdPostExceptionServiceImpl thirdPostExceptionServiceImpl;

    @Resource
    public void setThirdPostExceptionServiceImpl(ThirdPostExceptionServiceImpl thirdPostExceptionServiceImpl) {
        BusAssetApplyTouBiaoSetviceImpl.thirdPostExceptionServiceImpl = thirdPostExceptionServiceImpl;
    }

    @Resource
    public void setBusProcureApplyEntMapper(BusProcureApplyEntMapper busProcureApplyEntMapper) {
        BusAssetApplyTouBiaoSetviceImpl.busProcureApplyEntMapper = busProcureApplyEntMapper;
    }


    public static void doPostSzJiandeershi(ThirdPostInParm thirdPostInParm) {
        String url = "";
        //全量
        if (ConstantUtil.ALL_TYPE.equals(thirdPostInParm.getMethodType())) {
            url = ConstantUtil.ALL_THIRD_POST + 20;
        } else if (ConstantUtil.ADD_TYPE.equals(thirdPostInParm.getMethodType())) {
            //增量必须有时间
            if (StringUtils.isEmpty(thirdPostInParm.getStartTime()) &&
                    StringUtils.isEmpty(thirdPostInParm.getEndTime())) {
                return;
            }
            url = ConstantUtil.ADD_THIRD_POST + 20;
        } else {
            //更新必须有时间
            if (StringUtils.isEmpty(thirdPostInParm.getStartTime()) &&
                    StringUtils.isEmpty(thirdPostInParm.getEndTime())) {
                return;
            }
            url = ConstantUtil.UPDATE_THIRD_POST + 20;
        }
        try {
            //查询
            List<BusProcureApplyEnt> list = busProcureApplyEntMapper.selectListCha(thirdPostInParm);
            if (list == null || list.size() < 1) {
                log.info("投标单位数据库查询数据为0条");
                return;
            }
            if (list.size() > 3000) {
                Integer count = list.size() / 3000 + 1;
                Collection<Collection<BusProcureApplyEnt>> fgList = CommonUtil.subCollection(list, count);
                for (Collection<BusProcureApplyEnt> obj : fgList) {
                    sendData((List<BusProcureApplyEnt>) obj, url);
                }
            } else {
                sendData(list, url);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void sendData(List<BusProcureApplyEnt> obj, String url) {
        ResponseResult result = new ResponseResult();
        ArrayList list = new ArrayList();
        for (BusProcureApplyEnt a : obj) {
            BusAssetApplyTouBiaoParam param = new BusAssetApplyTouBiaoParam();
            param.setBiz_id(a.getId());
            param.setId(a.getId());
            param.setParent_id(a.getProcureApplyId());//ent的apply_id
            param.setCwh(a.getCwh());
            param.setCwhxzqh(CommonUtil.addZeroForNum(a.getFuorg(), 16));
            param.setName(a.getName());//ent.name
            param.setCode(a.getOrgCode());//ent.orgOrg
            param.setType(1);//没有字典
            param.setDel_flag(0);//0

            //转化为json字符串
            String jsonStr = JSONObject.toJSONString(param);
            //转化为json对象
            list.add(JSONObject.parseObject(jsonStr));
        }
        //发送(post)
        net.sf.json.JSONObject jsonObject = HttpRequestUtil.getHttpResponseJsonAllSSL(url, list);
        System.out.println("返回: " + jsonObject);
        if (jsonObject != null) {
            if ("200".equals(jsonObject.get("code"))) {
                result.setCode(200);
                result.setMsg("SUCCESS");
                result.setData("推送完成");
                log.info("投标单位表数据推送成功"+list.size(), result);
            } else {
                //记录日志
                thirdPostExceptionServiceImpl.insertExceptionData(
                        "doPostSzJiandeershi", "投标单位表数据",
                        jsonObject.getString("message"),
                        DateUtil.formatDate(DateUtil.FORMAT_YMD, new Date().getTime()));
                result.setCode(500);
                result.setMsg("ERROR");
                result.setData(jsonObject.getString("message"));
                log.info("投标单位推送失败", result);
            }
        } else {
            log.info("投标单位推送失败", "第三方返回空");
        }
    }

}
